Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
'Imports DotNetNuke.Installer
Imports ICSharpCode.SharpZipLib.Zip
Imports System.IO
Imports DotNetNuke
Imports System.Net

Namespace NVPortal.Modules.NVTuyenDung

    Public MustInherit Class Edit
        Inherits Entities.Modules.PortalModuleBase
        Dim tuyendung As String

#Region "Event Handlers"
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not IsPostBack Then
                Try
                    BindddlCongty()
                    BindddlViTri()
                    '<Edit news>
                    If Request.Item("tuyendung") <> "" Then
                        'Me.tdTinlienquan.Visible = True
                        Dim tuyendung As String
                        tuyendung = Request.Item("tuyendung")
                        ' Hien thi tin nay
                        Dim ctlNews As New NVTuyenDung_TuyenDungController
                        Dim objNews As NVTuyenDung_TuyenDUngInfo
                        objNews = ctlNews.NVTuyenDung_TuyenDung_GetByID(tuyendung, PortalId)
                        Me.txtTitle.Text = objNews.Title
                        Me.txtMaHoSo.Text = objNews.Maso
                        Me.txtNgayHetHan.Text = objNews.NgayHetHan.ToString("yyy/MM/dd")
                        Me.ddlCongty.SelectedValue = objNews.Congty
                        Me.ddlVitri.SelectedValue = objNews.Vitri
                        Me.chkHotSite.Checked = objNews.Hot
                        Me.txtHoSoGom.Text = objNews.Hoso
                        Me.txtyeuCau.Text = objNews.YeuCau
                        Me.txtMotacongviec.Text = objNews.MotaCongviec
                        Me.txtdiadiemlamviec.Text = objNews.Diadiemlamviec
                        Me.txtdiadiemnhanhoso.Text = objNews.Nhanhoso
                        Me.txtLienhe.Text = objNews.Lienhe
                        If objNews.Avatar.ToString.Contains("http://") Then
                            Me.imgNews.Visible = True
                            Me.imgNews.ImageUrl = PortalSettings.HomeDirectory & "TuyenDung/" & objNews.Avatar
                        Else
                            Me.imgNews.Visible = True
                            Me.imgNews.ImageUrl = PortalSettings.HomeDirectory & "TuyenDung/" & objNews.Avatar
                        End If
                    Else
                    End If
                    '</Edit news>
                Catch ex As Exception
                    ProcessModuleLoadException(Me, ex)
                End Try
            End If
        End Sub
        Private Sub lbtCancelBottom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtCancelBottom.Click
            Try
                Response.Redirect(NavigateURL(), True)
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Private Sub lbtUpdateTop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtUpdateTop.Click
            If addnews() Then
                'clearcontroldata()
                'addnewsvideo("idtintuc")
            End If
        End Sub
        Private Function addnews() As Boolean
            Try
                Dim ctlTuyenDung As New NVTuyenDung_TuyenDungController
                If Request.Item("tuyendung") <> "" Then
                    Dim tuyendung As String
                    tuyendung = Request.Item("tuyendung")
                    'update news

                    Dim stitle As String
                    Dim sMHS As String
                    Dim sHetHan As Date
                    Dim sCongty As String
                    Dim sVitri As String
                    Dim iHotsite As Integer
                    Dim sHoso As String
                    Dim SYeuCau As String
                    Dim sMota As String
                    Dim sDiadiemlv As String
                    Dim sdiadiemnhanhoso As String
                    Dim sLienhe As String
                    Dim iBL As Integer
                    Dim spicture As String
                    ' Insert

                    stitle = Me.txtTitle.Text
                    sMHS = Me.txtMaHoSo.Text
                    sHetHan = Me.txtNgayHetHan.Text
                    sCongty = Me.ddlCongty.SelectedValue
                    sVitri = Me.ddlVitri.SelectedValue
                    iHotsite = (CInt(IIf(Me.chkHotSite.Checked(), 1, 0)))
                    sHoso = Me.txtHoSoGom.Text
                    SYeuCau = Me.txtyeuCau.Text
                    sMota = Me.txtMotacongviec.Text
                    sDiadiemlv = Me.txtdiadiemlamviec.Text
                    sdiadiemnhanhoso = Me.txtdiadiemnhanhoso.Text
                    sLienhe = Me.txtLienhe.Text
                    iBL = CInt(IIf(Me.chkBL.Checked(), 1, 0))
                        If Me.imgNews.ImageUrl <> "" Then
                            spicture = Path.GetDirectoryName(imgNews.ImageUrl) & "/" & Path.GetFileName(imgNews.ImageUrl)
                        spicture = spicture.Replace("\Portals\" & PortalId & "\TuyenDung\", "")
                            spicture = spicture.Replace("\", "/")
                        Else
                        spicture = "/Portals/noavatar.jpg"
                        End If
                    ctlTuyenDung.NVTuyenDung_TuyenDung_Update(tuyendung, stitle, sMHS, sCongty, sVitri, sHoso, SYeuCau, sMota, sDiadiemlv, sdiadiemnhanhoso, sLienhe, DateTime.Now, sHetHan, iBL, iHotsite, 0, spicture)
                    Response.Redirect(NavigateURL(), True)
                Else
                    Dim iTuyenDung As String
                    Dim stitle As String
                    Dim sMHS As String
                    Dim sHetHan As Date
                    Dim sCongty As String
                    Dim sVitri As String
                    Dim iHotsite As Integer
                    Dim sHoso As String
                    Dim SYeuCau As String
                    Dim sMota As String
                    Dim sDiadiemlv As String
                    Dim sdiadiemnhanhoso As String
                    Dim sLienhe As String
                    Dim iBL As Integer
                    Dim spicture As String
                    ' Insert
                    iTuyenDung = Date.Now.ToString("yyyMMddHHmmss")
                    stitle = Me.txtTitle.Text
                    sMHS = Me.txtMaHoSo.Text
                    sHetHan = Me.txtNgayHetHan.Text
                    sCongty = Me.ddlCongty.SelectedValue
                    sVitri = Me.ddlVitri.SelectedValue
                    iHotsite = (CInt(IIf(Me.chkHotSite.Checked(), 1, 0)))
                    sHoso = Me.txtHoSoGom.Text
                    SYeuCau = Me.txtyeuCau.Text
                    sMota = Me.txtMotacongviec.Text
                    sDiadiemlv = Me.txtdiadiemlamviec.Text
                    sdiadiemnhanhoso = Me.txtdiadiemnhanhoso.Text
                    sLienhe = Me.txtLienhe.Text
                    iBL = CInt(IIf(Me.chkBL.Checked(), 1, 0))
                    If Me.imgNews.ImageUrl <> "" Then
                        spicture = Path.GetDirectoryName(imgNews.ImageUrl) & "/" & Path.GetFileName(imgNews.ImageUrl)
                        spicture = spicture.Replace("\Portals\" & PortalId & "\TuyenDung\", "")
                        spicture = spicture.Replace("\", "/")
                    Else
                        spicture = "/Portals/noavatar.jpg"
                    End If
                    ctlTuyenDung.NVTuyenDung_TuyenDung_Insert(iTuyenDung, stitle, sMHS, sCongty, sVitri, sHoso, SYeuCau, sMota, sDiadiemlv, sdiadiemnhanhoso, sLienhe, DateTime.Now, sHetHan, iBL, iHotsite, PortalId, UserId, 0, spicture)
                    Me.lbResult.Text = "Cập nhật thành công"
                    Response.Redirect(NavigateURL(), True)
                End If
                Return True
            Catch ex As Exception
                Me.lbResult.Text = "Lỗi cập nhật tin"
                ProcessModuleLoadException(Me, ex)
                Return False
            End Try
        End Function
        Private Sub lbtUpdateBottom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtUpdateBottom.Click
            If addnews() Then
                clearcontroldata()
                ' addnewsvideo("idtintuc")
            End If
        End Sub
        Private Sub lbtCancelTop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtCancelTop.Click
            Try
                If Request.Item("tuyendung") <> "" Then
                    'Dim tin As Integer
                    'tin = CInt(Request.Item("tin"))
                    Response.Redirect(NavigateURL(), True)
                Else
                    clearcontroldata()
                End If
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Private Sub clearcontroldata()
            
        End Sub
#End Region

        Private Sub lbtUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtUpload.Click
            Try
                If Page.IsPostBack Then
                    Dim PathtoWrite As String = Request.MapPath(PortalSettings.HomeDirectory) & "TuyenDung/"
                    Dim dateToUse As DateTime = DateTime.Now
                    Dim fullpath As String = String.Concat(PathtoWrite, "\", dateToUse.Year, "\", dateToUse.Month, "\", dateToUse.ToString("dd"), "\")
                    Dim shortpath As String = String.Concat(PortalSettings.HomeDirectory, "TuyenDung/", dateToUse.Year, "/", dateToUse.Month, "/", dateToUse.ToString("dd"), "/")
                    If Not System.IO.Directory.Exists(fullpath) Then
                        System.IO.Directory.CreateDirectory(fullpath)
                    End If
                    Dim strFileName As String
                    Dim strFileNamePath As String
                    Dim strExtension As String = ""
                    Dim strMessage As String = ""
                    Dim strChangeName As String
                    If Me.txtFile.PostedFile.FileName <> "" Then
                        strFileName = System.IO.Path.GetFileName(Me.txtFile.PostedFile.FileName)
                        strExtension = Path.GetExtension(strFileName)
                        strFileNamePath = System.IO.Path.GetFullPath(Me.txtFile.PostedFile.FileName)
                        strChangeName = strFileName.Replace(strFileName, DateTime.Now.Ticks & ".jpg")
                        'Me.txtFile.PostedFile.SaveAs(Request.MapPath(PortalSettings.HomeDirectory) & "News/" & strChangeName)
                        Me.txtFile.PostedFile.SaveAs(fullpath & strChangeName)
                        Me.imgNews.ImageUrl = shortpath & strChangeName
                        Me.imgNews.Visible = True
                        Me.lblMessage.Text = Request.Url.Host & shortpath & strChangeName
                    End If
                End If

            Catch exc As Exception 'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try

        End Sub
        Private Sub lbtDeleteTop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtDeleteTop.Click
            Try
                If Request.Item("tuyendung") <> "" Then
                    Dim tin As String
                    tin = Request.Item("tuyendung")
                    DataProvider.Instance.NV_TuyenDung_TuyenDung_Delete(tuyendung)
                    Response.Redirect(NavigateURL(), True)
                Else
                    clearcontroldata()
                End If
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Private Sub lbtDeleteBottom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtDeleteBottom.Click
            Try
                If Request.Item("tuyendung") <> "" Then
                    Dim tin As String
                    tin = Request.Item("tuyendung")
                    DataProvider.Instance.NV_TuyenDung_TuyenDung_Delete(tuyendung)
                    Response.Redirect(NavigateURL(), True)
                Else
                    clearcontroldata()
                End If
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Private Sub BindddlCongty()
            Dim ctlNewsCategories As New NVTuyenDung_CongtyController
            Dim arrNewsCategories As New ArrayList
            arrNewsCategories = ctlNewsCategories.NV_TuyenDung_Cty_GetAll(PortalId)
            Me.ddlCongty.DataSource = arrNewsCategories
            Me.ddlCongty.DataTextField = "TenCongty"
            Me.ddlCongty.DataValueField = "id"
            Me.ddlCongty.DataBind()
        End Sub
        Private Sub BindddlViTri()
            Dim ctlNewsCategories As New NVTuyenDung_VitriController
            Dim arrNewsCategories As New ArrayList
            arrNewsCategories = ctlNewsCategories.GetAll(PortalId)
            Me.ddlVitri.DataSource = arrNewsCategories
            Me.ddlVitri.DataTextField = "VitriTuyenDung"
            Me.ddlVitri.DataValueField = "id"
            Me.ddlVitri.DataBind()
        End Sub
    End Class

End Namespace
